<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
//  这个文件是 JCAT PHP框架的一部，该项目和此文件 均遵循 GNU 自由软件协议
// 
//  Copyleft 2008 JeCat.cn(http://team.JeCat.cn)
//
//
//  JCAT PHP框架 的正式全名是：Jellicle Cat PHP Framework。
//  “Jellicle Cat”出自 Andrew Lloyd Webber的音乐剧《猫》（《Prologue:Jellicle Songs for Jellicle Cats》）。
//  JCAT 是一个开源项目，它像音乐剧中的猫一样自由，你可以毫无顾忌地使用JCAT PHP框架。
//  JCAT 由中国团队开发维护，是一个国际性的项目。
//  正在使用的这个版本是：0.3.2 / SVN信息：$Id: class.JCAT_DBSQLRecordsOperate.php 1250 2009-02-07 04:53:17Z aleechou $
//
//
//
//  相关的链接：
//    [主页] http://jcat.JeCat.cn
//    [HTTP下载] http://jcat.JeCat.cn/download
//    [在线文档] http://jcat.JeCat.cn/document
//    [社区] http://jj.jecat.cn/forum-7-1.html
//  不很相关：
//    [MP3] http://www.google.cn/search?q=jellicle+songs+for+jellicle+cats+Andrew+Lloyd+Webber
//    [VCD/DVD] http://www.google.cn/search?q=CAT+Andrew+Lloyd+Webber+video
//
///////////////////////////////////////////////////////////////////////////////////////////////////////
/*-- Project Introduce --*/




/**
 * 多行操作（select,update,delete, 不包括 insert） 的父类
 *
 * @author		alee
 * @access		public
 */
abstract class JCAT_DBSQLRecordsOperate extends JCAT_DBSQL
{


	/**
	 * 数据表
	 * 
	 * @access	public
	 * @param	$aFactory		JCAT_DBAbstractFactory				对象工厂
	 * @param	$Tables			JCAT_DBGenericTypes::$TableList		数据表
	 * @return	
	 */
	public function JCAT_DBSQLRecordsOperate( $aFactory, $Tables=array() ) 
	{
		// 参数校验
		JCAT_ASSERT::ASSERT_INSTANCE($aFactory,'JCAT_DBAbstractFactory') ;
		
		// 创建 各个子句对象
		$this->aSubSQLFrom = JCAT_DBGenericTypes::TransTableList($aFactory,$Tables) ;
		$this->aSubSQLWhere = $aFactory->CreateSubSQLWhere() ;
		$this->aSubSQLLimit = $aFactory->CreateSubSQLLimit() ;
		$this->aSubSQLOrder = $aFactory->CreateSubSQLOrder() ;
		
		JCAT_ASSERT::ASSERT_INSTANCE($this->aSubSQLFrom,'JCAT_DBSubSQLFrom') ;
		JCAT_ASSERT::ASSERT_INSTANCE($this->aSubSQLWhere,'JCAT_DBSubSQLWhere') ;
		JCAT_ASSERT::ASSERT_INSTANCE($this->aSubSQLLimit,'JCAT_DBSubSQLLimit') ;
		JCAT_ASSERT::ASSERT_INSTANCE($this->aSubSQLOrder,'JCAT_DBSubSQLOrder') ;
		
			
		// “透明围栏” 内 的对象
		$this->arrObjectsInTransparent[] =& $this->aSubSQLWhere ;
		$this->arrObjectsInTransparent[] =& $this->aSubSQLFrom ;
		$this->arrObjectsInTransparent[] =& $this->aSubSQLLimit ;
		$this->arrObjectsInTransparent[] =& $this->aSubSQLOrder ;
		
		
		// 可设置 的对象
		$this->arrSetPropertys['SetSubSQLWhere'] =& $this->aSubSQLWhere ;
		$this->arrSetPropertys['SetSubSQLFrom'] =& $this->aSubSQLFrom ;
		$this->arrSetPropertys['SetSubSQLLimit'] =& $this->aSubSQLLimit ;
		$this->arrSetPropertys['SetSubSQLOrder'] =& $this->aSubSQLOrder ;
		
		$this->arrSetPropertys['SetStringSubSQLWhere'] =& $this->sSubSQLWhere ;
		$this->arrSetPropertys['SetStringSubSQLFrom'] =& $this->sSubSQLFrom ;
		$this->arrSetPropertys['SetStringSubSQLLimit'] =& $this->sSubSQLLimit ;
		$this->arrSetPropertys['SetStringSubSQLOrder'] =& $this->sSubSQLOrder ;
		
		// 可取得 的对象
		$this->arrGetPropertys['GetSubSQLWhere'] =& $this->aSubSQLWhere ;
		$this->arrGetPropertys['GetSubSQLFrom'] =& $this->aSubSQLFrom ;
		$this->arrGetPropertys['GetSubSQLLimit'] =& $this->aSubSQLLimit ;
		$this->arrGetPropertys['GetSubSQLOrder'] =& $this->aSubSQLOrder ;
		
		$this->arrGetPropertys['GetStringSubSQLWhere'] =& $this->sSubSQLWhere ;
		$this->arrGetPropertys['GetStringSubSQLFrom'] =& $this->sSubSQLFrom ;
		$this->arrGetPropertys['GetStringSubSQLLimit'] =& $this->sSubSQLLimit ;
		$this->arrGetPropertys['GetStringSubSQLOrder'] =& $this->sSubSQLOrder ;
		
		
		$this->SetFactory($aFactory) ;
	}

	/**
	 * 编译SQL子句
	 *
	 * @access	protected
	 * @param	$sSubSQLName	string	
	 * @return	string
	 */
	protected function MakeSubSQL($sSubSQLName)
	{
		JCAT_ASSERT::ASSERT_( array_key_exists('GetStringSubSQL'.$sSubSQLName, $this->arrGetPropertys)
				, JCAT_Language::SentenceEx('MakeSubSQL() 遇到意外的子句名称 %s','JCAT',null,$sSubSQLName))  ;
		
		$sStringSubSQL = call_user_func(array($this,'GetStringSubSQL'.$sSubSQLName)) ;
		$aObjectSubSQL = call_user_func(array($this,'GetSubSQL'.$sSubSQLName)) ;
		
		// 优先使用 字符类型的 子句
		if( $sStringSubSQL!==null )
		{
			return $sStringSubSQL ;
		}
		
		// 对象类型 子句
		else if( $aObjectSubSQL )
		{
			return $aObjectSubSQL->MakeSQL() ;
		}
		
		else
		{
			return '' ;
		}
	}


	public function __call($sMethod,$arrArgs)
	{
		// 设置 属性
		if( array_key_exists( $sMethod, $this->arrSetPropertys ) )
		{			
			// 参数校验
			JCAT_ASSERT::ASSERT_(isset($arrArgs[0])) ;
			
			if( (is_string($this->arrSetPropertys[$sMethod]) and is_string($arrArgs[0])) or
					get_class($this->arrSetPropertys[$sMethod]) == get_class($arrArgs[0]) )
			{
				$old_value = $this->arrSetPropertys[$sMethod] ;
				$this->arrSetPropertys[$sMethod] = $arrArgs[0] ;
				return $old_value ;
			}
			else
			{
				throw new JCAT_Exception( JCAT_Language::SentenceEx('请确保参数类型正确。','JCAT',null), JCAT_Exception::MakeExceptionCode(__CLASS__,1) ) ;
			}
		}
		
		// 获取 属性
		if( array_key_exists( $sMethod, $this->arrGetPropertys ) )
		{ return $this->arrGetPropertys[$sMethod] ; }
		
		/*if( preg_match('/^(Set|Get)(.+)/i',$sMethod,$arrRes) )
		{
			if( in_array($arrRes[2],$this->arrObjectsSetable) )
			{
				$sSubSQLName = 'a'.$arrRes[2] ;		
					
				// 设置
				if( strtolower($arrRes[1])==='set' )
				{
					// 参数校验
					JCAT_ASSERT::ASSERT_(isset($arrArgs[0])) ;
					JCAT_ASSERT::ASSERT_INSTANCE($arrArgs[0],'JCAT_DB'.$arrRes[2]) ;
					JCAT_ASSERT::ASSERT_INSTANCE($this->$sSubSQLName,'JCAT_DB'.$arrRes[2]) ;
					
					
					$old_value = $this->$sSubSQLName ;
					$this->$sSubSQLName = $arrArgs[0] ;
					return $old_value ;
				}
				
				// 返回
				else if( strtolower($arrRes[1])==='get' )
				{
					JCAT_ASSERT::ASSERT_INSTANCE($this->$sSubSQLName,'JCAT_DB'.$arrRes[2]) ;
					return $this->$sSubSQLName ;					
				}
				
				else
					JCAT_ASSERT::ASSERT_(false,'?!') ;
				
			}
		}*/
		
		
		
		// “透明围栏” 内 的对象
		foreach($this->arrObjectsInTransparent as $aObject)
		{//print_r( $aObject  );
			if( method_exists( $aObject, $sMethod ) )
				return call_user_func_array( array($aObject,$sMethod), &$arrArgs ) ;
		}
		
		
		
		
		throw new JCAT_Exception( JCAT_Language::SentenceEx('正在访问不存在的方法：%s','JCAT',null,$sMethod), JCAT_Exception::MakeExceptionCode(__CLASS__,2) ) ;
	}



	// 属性 ///////////////////////////////////////////////////////////////////////////////

	/**
	 * Where 子句
	 * 
	 * @access	private
	 * @var		JCAT_DBSubSQLWhere
	 */
	protected $aSubSQLWhere = null ;
	protected $sSubSQLWhere = null ;

	/**
	 * From 子句
	 * 
	 * @access	private
	 * @var		JCAT_DBSubSQLFrom
	 */
	protected $aSubSQLFrom = null ;
	protected $sSubSQLFrom = null ;

	/**
	 * Limit 子句
	 * 
	 * @access	private
	 * @var		JCAT_DBSubSQLLimit
	 */
	protected $aSubSQLLimit = null ;
	protected $sSubSQLLimit = null ;

	/**
	 * Order 子句
	 * 
	 * @access	private
	 * @var		JCAT_DBSubSQLOrder
	 */
	protected $aSubSQLOrder = null ;
	protected $sSubSQLOrder = null ;



	/**
	 * “透明围栏” 内 的对象
	 * 
	 * @access	protected
	 * @var		array
	 */
	protected $arrObjectsInTransparent = array() ;

	/**
	 * 可设置 的对象
	 * 
	 * @access	protected
	 * @var		array
	 */
	protected $arrSetPropertys = array() ;
	
	/**
	 * 可获取 的对象
	 * 
	 * @access	protected
	 * @var		array
	 */
	protected $arrGetPropertys = array() ;

}
/*macro_exception_code:2*/
?>